package com.shencai.demo.base.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shencai.demo.base.common.BaseContext;
import com.shencai.demo.base.entity.SysMenu;
import com.shencai.demo.base.service.SysMenuService;
import com.shencai.fw.exception.BaseException;
import com.shencai.fw.web.JsonResult;
import com.shencai.fw.web.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author:Wangym
 * @Date: 2022/10/18  9:51
 * @Version 1.0
 */
@RestController
@RequestMapping("/sysMenu")
public class SysMenuController {

    @Autowired
    private SysMenuService menuService;


    /**
     * 查询菜单树
     *
     * @return
     */
    @RequestMapping("/listMenuTree")
    public JsonResult<SysMenu> listMenuTree() {
        try {
            List<SysMenu> menus = menuService.listMenuTree();
            JsonResult<SysMenu> result = new JsonResult<SysMenu>(true, 200, "查询成功");
            result.setData(menus);
            return result;
        } catch (Exception exception) {
            throw new BaseException("查询异常");
        }
    }

    /**
     * 根据id查询下一级菜单
     *
     * @param id 菜单id
     * @return
     */
    @RequestMapping("/listNextLevelMenu")
    public JsonResult<SysMenu> listNextLevelMenu( Integer id) {
        try {
            List<SysMenu> menus = menuService.listNextMenus(id);
            JsonResult<SysMenu> result = new JsonResult<SysMenu>(true, 200, "查询成功");
            result.setData(menus);
            return result;
        } catch (Exception exception) {
            throw new BaseException("查询异常，请稍后重试~");
        }
    }

    /**
     * 根据菜单id分页查询下一级菜单
     * @param id
     * @param currentPage
     * @param pageSize
     * @return
     */
    @RequestMapping("/listNextLevelMenuByPage")
    public JsonResult<Page<SysMenu>> listNextLevelMenu(Integer id,Integer currentPage, Integer pageSize) {
        try {
            Page<SysMenu> menuPage = menuService.listNextMenusByPage(id, currentPage, pageSize);
            JsonResult<Page<SysMenu>> result = new JsonResult<Page<SysMenu>>(true, 200, "查询成功");
            result.setObj(menuPage);
            return result;
        } catch (Exception exception) {
            throw new BaseException("查询异常，请稍后重试~");
        }
    }

    /**
     * 根据id查询菜单
     *
     * @param id 菜单id
     * @return
     */
    @RequestMapping("/getMenu")
    public JsonResult<SysMenu> getMenuById(Integer id) {
        try {
            SysMenu menu = menuService.getById(id);
            JsonResult<SysMenu> result = new JsonResult<SysMenu>(true, 200, "查询成功");
            result.setObj(menu);
            return result;
        } catch (Exception exception) {
            throw new BaseException("查询异常，请稍后重试~");
        }
    }

    /**
     * 根据菜单名和菜单编码模糊查询菜单
     *
     * @param menu 前端接收的
     * @return
     */
    @RequestMapping("/listLikeMenu")
    public JsonResult<SysMenu> getMenuByNameAndCode(UserInfo userInfo, @RequestBody SysMenu menu) {
        try {
            List<SysMenu> menus = menuService.ListMenuByNameAndCode(menu.getName(), menu.getCode());
            JsonResult<SysMenu> result = new JsonResult<SysMenu>(true, 200, "查询成功");
            result.setData(menus);
            return result;
        } catch (Exception exception) {
            throw new BaseException("查询异常");
        }
    }

    /**
     * 根据菜单名和菜单编码模糊分页查询菜单
     * @param userInfo
     * @param menu
     * @param currentPage
     * @param pageSize
     * @return
     */
    @RequestMapping("/listLikeMenuByPage")
    public JsonResult<Page<SysMenu>> getMenuByNameAndCodeByPage(UserInfo userInfo, @RequestBody SysMenu menu,Integer currentPage, Integer pageSize) {
        try {
            Page<SysMenu> menus = menuService.ListMenuByNameAndCodeBypage(menu.getName(),menu.getCode(),currentPage,pageSize);
            JsonResult<Page<SysMenu>> result = new JsonResult<Page<SysMenu>>(true, 200, "查询成功");
            result.setObj(menus);
            return result;
        } catch (Exception exception) {
            throw new BaseException("查询异常");
        }
    }


    /**
     * 根据id删除菜单
     *
     * @param id
     * @return
     */
    @RequestMapping("/deleteMenu/{id}")
    public JsonResult deleteMenu(UserInfo userInfo, @PathVariable("id") Integer id) {
        try {
            BaseContext.setCurrentId(Integer.valueOf(userInfo.getUserId()));
            boolean remove = menuService.removeMenuById(id);
            if (remove) {
                JsonResult result = new JsonResult<SysMenu>(true, 200, "删除成功");
                return result;
            }
            JsonResult result = new JsonResult<SysMenu>(false, 444, "删除失败,请检查该菜单是否有子菜单");
            return result;
        } catch (Exception exception) {
            throw new BaseException("删除异常，请稍后重试~");
        }

    }

    /**
     * 添加菜单
     *
     * @param menu
     * @return
     */
    @RequestMapping("/addMenu")
    public JsonResult<SysMenu> addMenu(UserInfo userInfo,@RequestBody SysMenu menu) {
        try {
            BaseContext.setCurrentId(Integer.valueOf(userInfo.getUserId()));
            boolean save = menuService.saveMenu(menu);
            if (save) {
                JsonResult<SysMenu> result = new JsonResult<SysMenu>(true, 200, "添加成功");
                result.setObj(menu);
                return result;
            }
            JsonResult<SysMenu> result = new JsonResult<SysMenu>(false, 444, "添加失败,请检查惨淡编码和菜单编号是否重复！！！");
            return result;
        } catch (Exception exception) {
            throw new BaseException("添加异常，请稍后重试~");
        }
    }
    /**
     * 修改菜单信息
     *
     * @param menu
     * @return
     */
    @RequestMapping("/updateMenu")
    public JsonResult updateMenu(UserInfo userInfo, @RequestBody SysMenu menu) {
        try {
            BaseContext.setCurrentId(Integer.valueOf(userInfo.getUserId()));
            boolean update = menuService.updateById(menu);
            if (update) {
                JsonResult<SysMenu> result = new JsonResult<SysMenu>(true, 200, "修改成功");
                return result;
            }
            JsonResult<SysMenu> result = new JsonResult<SysMenu>(false, 444, "修改失败");
            return result;
        } catch (Exception exception) {
            throw new BaseException("修改失败，请稍后重试~");
        }
    }
}